package com.frikinet.lifeguard.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.frikinet.lifeguard.App;
import com.frikinet.lifeguard.events.UserSelectedEvent;
import com.frikinet.lifeguard.events.UserSelectedListener;
import com.frikinet.lifeguard.model.User;

public class UserNavigation extends JPanel implements UserSelectedListener
{
	private static final long serialVersionUID = 1L;
	private static final Logger log = LoggerFactory.getLogger(PostViewer.class);
	
	private Font clickFont, currentFont;
	
	private JLabel previousUser, nextUser;
	private JLabel currentUser;
	
	private User user;
	
	public UserNavigation()
	{
		super(new GridLayout(1, 3));
		
		App.events.addUserSelectedListener(this);
		
		Font defaultFont = UIManager.getDefaults().getFont("Label.font");
		clickFont = new Font(defaultFont.getName(), Font.BOLD, defaultFont.getSize()+2);
		currentFont = new Font(defaultFont.getName(), defaultFont.getStyle(), defaultFont.getSize()+2);
		
		buildGUI();
	}
	
	private void buildGUI()
	{
		setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		
		JPanel west = new JPanel(new FlowLayout(FlowLayout.LEFT));
		previousUser = setupClickLabel(YearNavigationType.PREVIOUS);
		west.add(previousUser);
		add(west);
		
		JPanel center = new JPanel(new FlowLayout(FlowLayout.CENTER));
		currentUser = new JLabel(" ");
		currentUser.setFont(currentFont);
		center.add(currentUser);
		add(center);
		
		JPanel east = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		nextUser = setupClickLabel(YearNavigationType.NEXT);
		east.add(nextUser);
		add(east);
	}
	
	private JLabel setupClickLabel(YearNavigationType type)
	{
		JLabel label = new JLabel(" ");
		label.setFont(clickFont);
		label.setForeground(Color.blue);
		label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		label.addMouseListener(new UserNavigationListener(type));
		return label;
	}
	
	@Override
	public void userSelected(UserSelectedEvent event)
	{
		List<User> users = new ArrayList<User>(App.users);
		user = event.getUser();
		
		currentUser.setText(user.getName());
		
		int index = users.indexOf(user);
		
		if(index > 0)
		{
			User previous = users.get(index-1);
			previousUser.setText("< "+previous.getName());
		} 
		else
		{
			previousUser.setText(" ");
		}
		
		if(index < users.size() - 1)
		{
			User next = users.get(index + 1);
			nextUser.setText(next+" >");
		}
		else
		{
			nextUser.setText(" ");
		}
	}
	
	private enum YearNavigationType
	{
		PREVIOUS, NEXT;
	}
	
	private class UserNavigationListener extends MouseAdapter
	{
		private YearNavigationType type;
		
		private UserNavigationListener(YearNavigationType type)
		{
			this.type = type;
		}

		@Override
		public void mouseReleased(MouseEvent e)
		{
			log.trace(String.format("User Navigation button clicked: %s", type));
			List<User> users = new ArrayList<User>(App.users);
			User user = UserNavigation.this.user;
			int index = users.indexOf(user);
			if(type==YearNavigationType.PREVIOUS)
			{
				index--;
			}
			else
			{
				index++;
			}
			App.events.fireUserSelected(new UserSelectedEvent(this, users.get(index)));
		}
	}
}
